package net.fpeg.kotlin.msa.wordbase.controller

import net.fpeg.kotlin.msa.wordbase.entity.WordSource
import net.fpeg.kotlin.msa.wordbase.service.WordSourceService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/wordSource")
class WordSourceController {
    @Autowired
    lateinit var wordSourceService: WordSourceService

    @GetMapping
    fun fetch(
            @RequestHeader("USER_ID") id: Long
    ): WordSource = wordSourceService.getTree(id)

    @PostMapping("/{wordSourceId}/{wordSourceValue}")
    @ResponseStatus(HttpStatus.CREATED)
    fun add(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("wordSourceId") wordSourceId: Long,
            @PathVariable("wordSourceValue") wordSourceValue: String) =
            wordSourceService.add(id, wordSourceId, wordSourceValue).id

    @PutMapping("/{wordSourceId}/{wordSourceValue}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun edit(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("wordSourceId") wordSourceId: Long,
            @PathVariable("wordSourceValue") wordSourceValue: String) {
        wordSourceService.edit(id, wordSourceId, wordSourceValue)
    }

    @DeleteMapping("/{wordSourceId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun remove(
            @RequestHeader("USER_ID") id: Long,
            @PathVariable("wordSourceId") wordSourceId: Long) {
        wordSourceService.remove(id, wordSourceId)
    }
}